package team.hyznrj.studentsys.controller;

import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import team.hyznrj.studentsys.entity.DataReport;
import team.hyznrj.studentsys.enums.DataReportEnum;
import team.hyznrj.studentsys.exception.DataBaseObjectNull;
import team.hyznrj.studentsys.exception.ZzfDataBaseException;

import java.util.HashMap;
import java.util.Map;
@ControllerAdvice
public class ExceptionController {

	/**
	 * 应用到所有@RequestMapping注解方法，在其执行之前初始化数据绑定器
	 * 
	 * @param binder
	 */
	@InitBinder
	public void initBinder(WebDataBinder binder) {
	}

	/**
	 * 把值绑定到Model中，使全局@RequestMapping可以获取到该值
	 * 
	 * @param model
	 */
	@ModelAttribute
	public void addAttributes(Model model) {
		
	}
	/**
	 * 异常处理
	 * @param ex
	 * @return
	 */
	@ResponseBody
	@ExceptionHandler(value = ZzfDataBaseException.class)
	public DataReport errorHadler(ZzfDataBaseException ex){
		DataReport dataReport = new DataReport();
		dataReport.setCode(ex.getCode());
		dataReport.setFlag(false);
		dataReport.setReport(ex.getMsg());
		return dataReport;
	}
}
